﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.CSharp.Services.Language.Interop;
using Microsoft.RestrictedUsage.CSharp.Semantics;
using Microsoft.RestrictedUsage.CSharp.Extensions;


namespace devtm.Documentation.CSharpModel
{

    public abstract class CSharpTypeBaseItem : CSharpBaseItem
    {
        // Fields
        private string _fullName;
        private string _title;
        private CSharpTypeReference _type;
        private static readonly CSharpNameFormatter TitleFormatter;

        // Methods
        static CSharpTypeBaseItem()
        {
            CSharpNameFormatter formatter = new CSharpNameFormatter
            {
                IncludeNamespace = false
            };
            TitleFormatter = formatter;
        }

        protected CSharpTypeBaseItem(CSharpTypeReference type, object parent)
            : base(parent)
        {
            this._type = type;
        }

        //public override bool Invoke()
        //{
        //    try
        //    {
        //        IList<SourceLocation> sourceLocations = this._type.Type.SourceLocations;
        //        if (sourceLocations.Count > 0)
        //        {
        //            foreach (SourceLocation location in sourceLocations)
        //            {
        //                DisconnectedSourceLocation selectedSourceLocation = new DisconnectedSourceLocation(location);
        //                CSharpTextSelection.NavigateToSourceLocation(selectedSourceLocation);
        //                break;
        //            }
        //        }
        //        else
        //        {
        //            CSharpUsageMetrics.RaiseMetadataViewAttempted(this);
        //            MessageBox.Show("We're sorry, but navigating to this definition is not currently supported.", "Solution Navigator");
        //        }
        //        return true;
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}

        // Properties
        public XMLDocInfo Documentation
        {
            get
            {

                if (this._type.Type.XMLDocumentation != null)                
                    return this._type.Type.XMLDocumentation;
                
                return null;
            }
        }

        public string FullName
        {
            get
            {
                try
                {
                    if (this._fullName == null)
                    {
                        this._fullName = this._type.Type.GetFullTypeName();
                    }
                    return this._fullName;
                }
                catch
                {
                    return "Error: Please collapse and re-expand the parent source file.";
                }
            }
        }

        //public override ImageSource Icon
        //{
        //    get
        //    {
        //        StandardGlyphGroup glyphGroupDelegate;
        //        StandardGlyphItem glyphItemProtected;
        //        CSharpType type = this._type.Type;

        //        if (type.IsDelegate)
        //            glyphGroupDelegate = StandardGlyphGroup.GlyphGroupDelegate;

        //        else if (type.IsEnum)
        //            glyphGroupDelegate = StandardGlyphGroup.GlyphGroupEnum;

        //        else if (type.IsInterface)
        //            glyphGroupDelegate = StandardGlyphGroup.GlyphGroupInterface;

        //        else if (type.IsPredefinedType)
        //            glyphGroupDelegate = StandardGlyphGroup.GlyphGroupIntrinsic;

        //        else if (type.IsStruct)
        //            glyphGroupDelegate = StandardGlyphGroup.GlyphGroupStruct;

        //        else if (type.IsValueType)
        //            glyphGroupDelegate = StandardGlyphGroup.GlyphGroupValueType;

        //        else if (type.IsClass)
        //            glyphGroupDelegate = StandardGlyphGroup.GlyphGroupClass;

        //        else
        //            glyphGroupDelegate = StandardGlyphGroup.GlyphGroupType;




        //        if (type.IsProtected)
        //            glyphItemProtected = StandardGlyphItem.GlyphItemProtected;

        //        else if (type.IsPrivate)
        //            glyphItemProtected = StandardGlyphItem.GlyphItemPrivate;

        //        else if (type.IsInternal)
        //            glyphItemProtected = StandardGlyphItem.GlyphItemInternal;

        //        else                
        //            glyphItemProtected = StandardGlyphItem.GlyphItemPublic;
                
        //        return base.GetGlyph(glyphGroupDelegate, glyphItemProtected);
        //    }
        //}

        public override string Title
        {
            get
            {
                try
                {
                    if (this._title == null)
                    {
                        this._title = TitleFormatter.FormatTypeName(this._type.Type);
                    }
                    return this._title;
                }
                catch
                {
                    return "Error: Please collapse and re-expand the parent source file.";
                }
            }
        }

        public CSharpTypeReference TypeReference
        {
            get
            {
                return this._type;
            }
        }
    }



}
